package com.test.test

//# Person.groovy
class Person {
    String name
    int age

    void eat(String food) {
        println("你喂的${food}真难吃")
    }

    @Override
    String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}'
    }

}

class Main {
    void eat(String food){
        println "我根本不会吃，不要喂我${food}"
    }
    def cc = {
        name = "hanmeimei"
        age = 26
        eat("油条")
    }
//
//    Closure.OWNER_FIRST是默认策略。优先在owner寻找，owner没有再delegate
//
//    Closure.DELEGATE_FIRST：优先在delegate寻找，delegate没有再owner
//
//    Closure.OWNER_ONLY：只在owner中寻找
//
//    Closure.DELEGATE_ONLY：只在delegate中寻找
//
//    Closure.TO_SELF：暂时没有用到，哎不知道啥意思
    // lxk: 在闭包里修改Person的name和age，还想调用eat方法
    static void main(String... args) {
        com.test.test.Main main = new com.test.test.Main()
        com.test.test.Person person = new com.test.test.Person(name: "lilei", age: 14)
        println person.toString()

//        #打印结果
//        Person{name='lilei', age=14}
//        你喂的油条真难吃
//        Person{name='hanmeimei', age=26}


        main.cc.delegate = person
//        main.cc.setResolveStrategy(Closure.DELEGATE_FIRST)
        main.cc.setResolveStrategy(Closure.OWNER_FIRST)
        main.cc.call()
        println person.toString()
    }
}

